package csu.edu.flatprogram.service.impl;


import csu.edu.flatprogram.common.CommonResponse;
import csu.edu.flatprogram.entity.Category;
import csu.edu.flatprogram.entity.Item;
import csu.edu.flatprogram.entity.ItemInventory;
import csu.edu.flatprogram.entity.Product;
import csu.edu.flatprogram.persistence.CategoryMapper;
import csu.edu.flatprogram.persistence.ItemInventoryMapper;
import csu.edu.flatprogram.persistence.ItemMapper;
import csu.edu.flatprogram.persistence.ProductMapper;
import csu.edu.flatprogram.service.CatalogService;
import csu.edu.flatprogram.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("catalogService")
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemInventoryMapper itemInventoryMapper;

    @Override
    public CommonResponse<List<Category>> getCategoryList(){
       //此处省略了参数验证
        List<Category> categoryList = categoryMapper.selectList(null);
        if (categoryList.isEmpty()){
            return CommonResponse.createForSuccessMessage("没有分类数据");
        }
        return CommonResponse.createForSuccess(categoryList);
    }
    @Override
    public CommonResponse<Category> getCategory(String categoryId){
        Category category = categoryMapper.selectById(categoryId);
        if (category==null){
            return CommonResponse.createForSuccessMessage("没有数据");
        }
        return CommonResponse.createForSuccess(category);
    }

    //根据ID搜索商品
    @Override
    public CommonResponse<ItemVO> getItemById(String itemId){
        Item item = itemMapper.selectById(itemId);
        if(item==null){
            return CommonResponse.createForError("没有该item的信息");
        }

        Product product = productMapper.selectById(item.getProductId());
        ItemVO itemVO = itemToItemVO(item,product);
        return CommonResponse.createForSuccess(itemVO);
    }

    private ItemVO itemToItemVO(Item item, Product product) {
        ItemVO itemVO = new ItemVO();
        itemVO.setItemId(item.getItemId());
        itemVO.setProductId(item.getProductId());
        itemVO.setListPrice(item.getListPrice());
        itemVO.setUnitCost(item.getUnitCost());
        itemVO.setStatus(item.getStatus());
        itemVO.setAttribute1(item.getAttribute1());
        itemVO.setDescn(item.getDescn());
        itemVO.setDescnpic(item.getDescnpic());
        itemVO.setItemName(item.getItemName());

        itemVO.setCategoryId(product.getCategoryId());
        itemVO.setProductName(product.getName());
        itemVO.setProductDescription(product.getDescription());

        ItemInventory itemInventory = itemInventoryMapper.selectById(item.getItemId());
        itemVO.setQuantity(itemInventory.getQuantity());

        return itemVO;
    }


}
